import Loading from './loading';
/**
 * 列表页面对象
 */
function PageData() {
    return {
        query: {
            page: 0,
            size: 20,
            sort: '',
            order: '',
        },
        page: {
            size: 20,
            totalElements: 0,
            totalPages: 0,
            number: 0
        },
        list: [],
        loading: 0,
        response: {
            _embedded: {},
            _links: {},
            page: {
                size: 20,
                totalElements: 0,
                totalPages: 0,
                number: 0
            }
        },
        hasNext: true,
        options: {
            pullDownRefresh: { txt: '刷新完成' },
            pullUpLoad: { txt: '加载成功' }
        }
    };
}

/**
 * 列表页方法对象
 * @param API
 * @param repository
 * @constructor
 */
function PageMethods(API, repository) {
    return {
        /**
         * 获取默认查询条件
         * @returns {{sort: string}}
         */
        getQuery() {
            return { ...this.query, sort: 'gmtCreate,desc' };
        },
        getPage(query) {
            Loading.up();
            API.fetchPage(this.getQuery())
                .then(res => {
                    Loading.down();
                    this.response = res;
                    this.assignList(res);
                    if (this.response.totalPages <= this.response.number) {
                        this.hasNext = false;
                    }
                })
                .catch(() => {
                    Loading.down();
                });
        },
        assignList(res) {
            this.list = this.list.concat(res.content);
        },
        onPullingDown() {
            this.$refs.scroll.forceUpdate();
        },
        onPullingUp() {
            if (this.hasNext) {
                this.query.page++;
                this.getPage(this.getQuery());
            } else {
                this.$refs.scroll.forceUpdate();
            }
        },
        // 编辑
        onEdit: function (id) {
            this.$router.push(`/${repository}/${id}?edit=1`);
        },
        // 添加
        onAdd: function () {
            this.$router.push(`/${repository}/0?edit=1`);
        },
        // 删除
        onDelete: function (index, row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {}).then(() => {
                this.loading++;
                API.deleteEntity(row.id)
                    .then(() => {
                        this.loading--;
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getPage();
                    })
                    .catch(() => {
                        this.loading--;
                    });
            });
        }
    };
}

/**
 * 编辑页面数据对象
 * @returns {{loading: number, model: {}}}
 * @constructor
 */
function EditData() {
    return {
        loading: 0,
        model: {}
    };
}

/**
 * 编辑页面计算属性对象
 * @returns {*}
 * @constructor
 */
function EditComputed() {
    return {
        id() {
            return this.$route.params.id === '0' ? null : this.$route.params.id;
        }
    };
}

/**
 * 编辑页面方法对象
 * @param API
 * @returns {*}
 * @constructor
 */
function EditMethods(API) {
    return {

        /**
         * 初始化页面
         */
        init() {
            if (this.id) {
                this.getModel(this.id);
            } else {
                this.newModel();
            }
        },
        /**
         * 新建实体
         * @returns {{id: null, name: string}}
         */
        newEntity() {
            return {
                id: null,
                name: '',
            };
        },
        /**
         * 获取对象
         * @param id
         */
        getModel(id) {
            Loading.up();
            API.getModel(id)
                .then(res => {
                    Loading.down();
                    this.assignModel(res);
                }).catch(() => {
                    Loading.down();
                });
        },
        /**
         * 转换数据格式，
         * @param res
         */
        assignModel(res) {
            this.model = Object.assign(this.model, res);
        },
        /**
         * 保存数据
         */
        onSave(e) {
            this.beforeSubmit();
            Loading.up();
            // 提交录入信息
            API.save(this.model)
                .then(res => {

                })
                .catch(_ => {
                    Loading.down();
                });
            e.preventDefault();
        },
        /**
         * 提交之前的操作
         */
        beforeSubmit() {
            // 检查是否新建，新建时填入提交人
        },
        /**
         * 保存之后的操作
         * @param res
         */
        afterSaved(res) {
            Loading.down();
            this.$createToast({
                type: 'correct',
                txt: '保存成功'
            }).show();
            this.$router.go(-1);
        }
    };
}

export const ListCrud = {
    data: PageData,
    methods: PageMethods,
};

export const EditCrud = {
    data: EditData,
    computed: EditComputed,
    methods: EditMethods,
};